import { readFileSync, readFile } from 'fs-extra';
import { join,extname } from 'path';
import Vue from 'vue/dist/vue';
import http from "http";
const component = Vue.extend({
    template: readFileSync(join(__dirname, '../../../static/template/vue/app.html'), 'utf-8'),
    data() {
        return {
          
        };
    },
    methods: {

    },
    async mounted() {
        const path = join(__dirname, '../../../static/cocos');
        const server = http.createServer((req:any, res) => {
            // 获取请求的文件名（包括扩展名）
            let filePath = join(path, req.url === '/' ? 'index.html' : req.url);
            // 获取文件扩展名
            let extnamea = extname(filePath);
          
            // 默认的内容类型
            let contentType = 'text/html';
          
            // 设置 MIME 类型
            switch (extnamea) {
              case '.js':
                contentType = 'text/javascript';
                break;
              case '.css':
                contentType = 'text/css';
                break;
              // 可以根据需要添加更多文件类型
            }
          
            // 读取并返回文件
            readFile(filePath, (err, content) => {
              if (err) {
                if (err.code == 'ENOENT') {
                  // 页面未找到
                 readFile(join(__dirname, '404.html'), (err, content) => {
                    res.writeHead(404, { 'Content-Type': 'text/html' });
                    res.end(content, 'utf-8');
                  });
                } else {
                  // 服务器错误
                  res.writeHead(500);
                  res.end(`Server Error: ${err.code}`);
                }
              } else {
                // 成功读取文件
                res.writeHead(200, { 'Content-Type': contentType });
                res.end(content, 'utf-8');
              }
            });
          });
          
          server.listen(3000, () => {
            console.log('杨宗宝：游戏服务已开启...');
          });
    },
});
const panelDataMap = new WeakMap() as WeakMap<object, InstanceType<typeof component>>;
/**
 * @zh 如果希望兼容 3.3 之前的版本可以使用下方的代码
 * @en You can add the code below if you want compatibility with versions prior to 3.3
 */
// Editor.Panel.define = Editor.Panel.define || function(options: any) { return options }
module.exports = Editor.Panel.define({
    listeners: {
      
    },
    template: readFileSync(join(__dirname, '../../../static/template/default/index.html'), 'utf-8'),
    style: readFileSync(join(__dirname, '../../../static/style/default/index.css'), 'utf-8'),
    $: {
        app: '#app',
    },
    methods: {
        hello() {

        },
    },
    ready() {
        if (this.$.app) {
            const vm = new component();
            panelDataMap.set(this, vm);
            vm.$mount(this.$.app);
        }
    },
    beforeClose() { },
    close() {
        const vm = panelDataMap.get(this);
        if (vm) {
            vm.$destroy();
        }
    },
});
